/**
 * Created by DevilsEyes on 2016/4/19.
 */
'use strict';

var $course = require('../../db/course.js');

let filter = function (o) {
    return {
        _id: o._id,
        title: o.title,//标题

        tags: o.tags || '',//标签  A#B#C#D
        view: o.view || 0,//浏览量
        class: o.class || '',//班级  A#B#C#D

        imgs: o.imgs.map((i)=>JSON.parse(i)),//图片 文件JSON字符串组
        article: o.article || '',//内容(允许换行\n)
        attachments: o.attachments.map((i)=>JSON.parse(i)),//附件 文件JSON字符串组

        updated: o.updated
    }
};

module.exports = {

    save: (obj) => {
        obj.imgs = obj.imgs.map((i)=> {
            return JSON.stringify(i);
        });
        obj.attachments = obj.attachments.map((i)=> {
            return JSON.stringify(i);
        });
        var course_Entity = new $course(obj);

        return new Promise((O, X)=> {
            course_Entity.save(()=> {
                O();
            })
        })
    },
    getOnebyId(id){
        return new Promise((O, X)=> {
            $course
                .find({_id: id})
                .then((courses)=> {

                    if (courses && courses.length > 0) {
                        O(filter(courses[0]));
                    } else {
                        throw {
                            code: 9999,
                            msg: '未找到该课件'
                        }
                    }

                });
        })
    },
    update: (id, o) => {
        var cp = {
            title: o.title,//标题
            article: o.article || '',//内容(允许换行\n)
            tags: o.tags || '',//标签  A#B#C#D
            class: o.class || '',//班级  A#B#C#D
            imgs: o.imgs.map((i)=>JSON.stringify(i)),//图片 文件JSON字符串组
            attachments: o.attachments.map((i)=>JSON.stringify(i))//附件 文件JSON字符串组
        };

        return new Promise((O, X)=> {
            $course
                .find({_id: id})
                .update(cp)
                .then(()=> {
                    O();
                })
        })
    },
    deleteById: (id)=> {
        return new Promise((O, X)=> {
            $course
                .remove({_id: id})
                .then(()=> {
                    O();
                })
        })
    },

    //查重
    // isRepeated(link){
    //     return new Promise((done, fail)=> {
    //         $course
    //             .findOne({link: link})
    //             .then((err, course) => {
    //                 if (err)
    //                     fail(err);
    //                 else if (course)
    //                     fail(new Error('重复的链接'));
    //                 else
    //                     done();
    //             });
    //     })
    // },
    //
    // getSome: (limit) => {
    //     return $course
    //         .find({status: 0})
    //         .sort({deep: 'desc', id: 'asc'})
    //         .limit(limit)
    // },


    getAll: () => {
        return new Promise((O, X)=> {
            $course
                .find({})
                .then((courses)=> {
                    try {
                        O(courses.map((i)=>filter(i)));
                    }
                    catch (e) {
                        console.log(e);
                        X({
                            code: 9999,
                            msg: e
                        });
                    }
                });
        })
    },

    clear: () => {
        return new Promise((O, X)=> {
            $course
                .remove({})
                .then(()=> {
                    O();
                });
        })
    }

};